package com.msmall.community.growcycle.preparer;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.dpt.model.Segment;

public class HotPreparer implements ViewPreparer{
	private static final Log log = LogFactory.getLog(HotPreparer.class);
	
	@SuppressWarnings("unchecked")
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			CascadeCollection<Segment> navSegments = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_TIMELINE_NAV);
			List<CascadeCollection<Segment>> lists = new ArrayList<CascadeCollection<Segment>>();
			for(CascadeCollection<Segment> parent : navSegments.getChildren()){
				if(parent != null){
					lists.add(parent);
					
					if(parent.getChildren() != null){
						if(parent.getChildren().size() > 6){
							lists.addAll(parent.getChildren().subList(0, 6));
						}else{
							lists.addAll(parent.getChildren());
						}
					}
				}
			}
			request.setAttribute("growCycleHots", lists);
		}catch(Exception e){
			log.debug(e);
		}
	}
}
